package com.csot.ry.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.csot.common.annotation.Log;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.AjaxResult;
import com.csot.common.enums.BusinessType;
import com.csot.ry.domain.TAssetIp;
import com.csot.ry.service.ITAssetIpService;
import com.csot.common.utils.poi.ExcelUtil;
import com.csot.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产ipController
 *
 * @author csot
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/asset/ip")
public class TAssetIpController extends BaseController {
    @Autowired
    private ITAssetIpService tAssetIpService;

    /**
     * 查询资产ip列表
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAssetIp tAssetIp) {
        startPage();
        List<TAssetIp> list = tAssetIpService.selectTAssetIpList(tAssetIp);
        return getDataTable(list);
    }

    /**
     * 导出资产ip列表
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:export')")
    @Log(title = "资产ip", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAssetIp tAssetIp) {
        List<TAssetIp> list = tAssetIpService.selectTAssetIpList(tAssetIp);
        ExcelUtil<TAssetIp> util = new ExcelUtil<TAssetIp>(TAssetIp.class);
        util.exportExcel(response, list, "资产ip数据");
    }

    /**
     * 获取资产ip详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tAssetIpService.selectTAssetIpById(id));
    }

    /**
     * 新增资产ip
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:add')")
    @Log(title = "资产ip", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TAssetIp tAssetIp) {
        return toAjax(tAssetIpService.insertTAssetIp(tAssetIp));
    }

    /**
     * 修改资产ip
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:edit')")
    @Log(title = "资产ip", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TAssetIp tAssetIp) {
        return toAjax(tAssetIpService.updateTAssetIp(tAssetIp));
    }

    /**
     * 删除资产ip
     */
    @PreAuthorize("@ss.hasPermi('asset:ip:remove')")
    @Log(title = "资产ip", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tAssetIpService.deleteTAssetIpByIds(ids));
    }


    // 导入方法
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TAssetIp> util = new ExcelUtil<TAssetIp>(TAssetIp.class);
        List<TAssetIp> xxxxList = util.importExcel(file.getInputStream());
        String message = tAssetIpService.importData(xxxxList, updateSupport);
        return AjaxResult.success(message);
    }

    // 下载模板
    @PostMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response) {
//        ExcelUtil<TAssetIp> util = new ExcelUtil<TAssetIp>(TAssetIp.class);
//        return util.importTemplateExcel("资产数据");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        try {
            String path = System.getProperty("user.dir");
            path = path + File.separator + "Template" + File.separator + "assetip.xlsx";
            IOUtils.write(Files.readAllBytes(Paths.get(path)), response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error("下载ip段模块异常", e);
        }
    }

}
